package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.model.MapSetInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;

public class MapSettingActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "MapSettingActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, MapSettingActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapsetting_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_mapdir;
    private TextView tv_wgdistance;
    private ImageView iv_swith;
    private ImageView ivIcon;
    private ImageView iv_swith_gcj02;

    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_mapdir = (TextView) findViewById(R.id.tv_mapdir);
        iv_swith = (ImageView) findViewById(R.id.iv_swith);
        tv_wgdistance = (TextView) findViewById(R.id.tv_wgdistance);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        iv_swith_gcj02 = (ImageView) findViewById(R.id.iv_swith_gcj02);
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    MapSetInfo _setInfo;

    @Override
    public void initData() {//必须调用
        _setInfo = MapSetInfo.getInstance();
        _setInfo.load(this);
        refresh();
    }

    void refresh() {
        tv_mapdir.setText(_setInfo.getMapdirmodeStr());
        String AA[] = SysUtils.getInstance().getAltitudeStr(_setInfo.getWgdistance());
        tv_wgdistance.setText(AA[0] + AA[1]);

        if (_setInfo.isUsewg()) {
            iv_swith.setImageResource(R.drawable.swith_open);
        } else {
            iv_swith.setImageResource(R.drawable.swith_close);
        }

        if (_setInfo.getCaricontype() == 0) {
            ivIcon.setImageResource(R.drawable.i3_car00);
        } else if (_setInfo.getCaricontype() == 1) {
            ivIcon.setImageResource(R.drawable.i3_car3d);
        }

        if (_setInfo.isgcj02()) {
            iv_swith_gcj02.setImageResource(R.drawable.swith_open);
        } else {
            iv_swith_gcj02.setImageResource(R.drawable.swith_close);
        }

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.mapset_0).setOnClickListener(this);
        findViewById(R.id.mapset_1).setOnClickListener(this);
        findViewById(R.id.mapset_2).setOnClickListener(this);
        findViewById(R.id.mapset_3).setOnClickListener(this);
        findViewById(R.id.mapset_4).setOnClickListener(this);
        findViewById(R.id.mapset_5).setOnClickListener(this);

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        _setInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    private int[] CTRL_IDS = {
            R.id.mapset_0, R.id.mapset_1, R.id.mapset_2,
            R.id.mapset_4, R.id.mapset_5};

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mapset_0:
                toActivity(BottomMenuWindow.createIntent(context, MapSetInfo.MODEDIR_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.mapdir)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case R.id.mapset_1:
                onUserWg();
                break;
            case R.id.mapset_2:
                editWgDistnce();
                break;
            case R.id.mapset_3:
                break;
            case R.id.mapset_4:
                changeCarIcon();
                break;
            case R.id.mapset_5:
                changeGcj02();
                break;
            default:
                break;
        }
    }

    private void changeGcj02() {
        boolean isgcj02 = _setInfo.isgcj02();
        _setInfo.setIsgcj02(!isgcj02);
        refresh();
    }

    private void changeCarIcon() {
        toActivity(SelectIconActivity.createIntent(context, 1), SELECTICON);
    }

    private void onUserWg() {
        boolean usewg = _setInfo.isUsewg();
        _setInfo.setUsewg(!usewg);
        refresh();
    }

    private void editWgDistnce() {

        String strUnit = "m";
        double distance = _setInfo.getWgdistance();
        switch (SysUtils.getInstance().dis_mode) {
            default:
                break;
            case 4:
            case 1:
                distance = distance * 3.2808399;
                strUnit = "ft";
                break;
            case 2:
                distance = distance * 1.0936133;
                strUnit = "yd";
                break;
        }


        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.wgjl), distance, strUnit);
        toActivity(intent, REQUEST_TO_WGJL);
    }

    private final int REQUEST_TO_WGJL = 23;
    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;
    private final int SELECTICON = 32;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECTICON) {
            _setInfo.setCaricontype(resultCode);
            refresh();
            return;
        }

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    _setInfo.setMapdirmode(selectedPosition);
                    _setInfo.save(this);
                    refresh();
                }
                break;
            case REQUEST_TO_WGJL:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    double distance = Double.valueOf(editedValue);

                    switch (SysUtils.getInstance().dis_mode) {
                        default:
                            break;
                        case 1:
                        case 4:
                            //ft => m
                            distance = distance / 3.2808399;
                            break;
                        case 2: {
                            //yd => m
                            distance = distance / 1.0936133;
                        }
                        break;
                    }
                    _setInfo.setWgdistance(distance);

                    _setInfo.save(this);
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _setInfo.reset();
                            _setInfo.save(this);
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
